﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Web;

namespace ControlExplorer
{
  public static class DBHelper
  {
    public static DbConnection CreateConnection(string sqlConString, out DbProviderFactory factory)
    {
      factory = DbProviderFactories.GetFactory("System.Data.OleDb");
      DbConnection connection = factory.CreateConnection();
      connection.ConnectionString = sqlConString;
      return connection;
    }

    public static DbConnection CreateConnection(string sqlConString)
    {
      DbProviderFactory factory;
      return CreateConnection(sqlConString, out factory);
    }
  }

  public static class Strings
  {
    public static string FormatCurrency(object value, int decimalPlaces)
    {
      if (Information.IsNumeric(value))
      {
        double d = Convert.ToDouble(value);
        return d.ToString(string.Concat("C", decimalPlaces));
      }
      return string.Empty;
    }

    public static string FormatDateTime(object value)
    {
      if (value is DateTime)
      {
        return ((DateTime)value).ToShortDateString();
      }
      return string.Empty;
    }
  }

  public static class Information
  {
    public static bool IsArray(object o)
    {
      return o is Array;
    }

    public static bool IsDBNull(object o)
    {
      return Convert.IsDBNull(o);
    }

    public static int UBound(object o)
    {
      if (o is Array)
        return ((Array)o).GetUpperBound(0);
      else
        return -1;
    }
    public static int LBound(object o)
    {
      if (o is Array)
        return ((Array)o).GetLowerBound(0);
      else
        return -1;
    }

    public static bool IsNumeric(object o)
    {
      double d;
      return o != null ? double.TryParse(o.ToString(), out d) : false;
    }
  }
}